package model

import (
	"fmt"
	"strings"
	"time"
)

type WechatMoments struct {
	Id int64 `gorm:"primaryKey"`
	UserId int64
	UserName string `gorm:"index"`
	WhoCanSeeItType int
	WhoCanSeeItList string `gorm:"type:text"`
	CreatedAtUnix int64
	CreatedAt time.Time
	UpdatedAt time.Time
}

func (wechatMoments *WechatMoments)BubbleSort(arr []*WechatMoments) []*WechatMoments{
	for i := 0; i < len(arr)-1; i++ {
		for j := 0; j < len(arr)-i-1; j++ {
			if arr[j].CreatedAtUnix < arr[j+1].CreatedAtUnix {
				arr[j], arr[j+1] = arr[j+1], arr[j]
			}
		}
	}
	return arr
}

func (wechatMoments *WechatMoments)ViewYourCircleOfGriends(uId string, pageSize int, pageNo int) ([]*WechatMoments, error) {

	tx := Db.Begin()

	defer func(){
		err := recover()
		if err != nil{
			tx.Rollback()
		}
	}()

	var me Users
	tx.Where("id = ?", uId).First(&me)

	var wmList []*WechatMoments

	var uList []*Users
	tx.Joins("JOIN friends ON friends.friend_id = users.id AND friends.own_id =?", uId).Not("users.id", strings.Split(me.DontLookAtHim, ",")).Find(&uList)
	fmt.Println("uList：", uList)


	for index, u := range uList{
		for _, nuId:= range strings.Split(u.DontLetHimSee, ","){
			if nuId == uId{
				fmt.Println("nuId：", nuId)
				uList = append(uList[:index], uList[index+1:]...)
				break
			}
		}
	}

	for _, u := range uList{

		var uwmList []*WechatMoments

		if u.AllowFriendsToViewTheCircleOfFriends == 1{
			tx.Where("user_id = ?", u.Id).Where("who_can_see_it_type <> ?", 2).Find(&uwmList)
		}else if u.AllowFriendsToViewTheCircleOfFriends == 2 {
			tx.Where("user_id = ?", u.Id).Where("who_can_see_it_type <> ?", 2).Where("created_at >= ?", time.Now().AddDate(0, 0, -3).Format("2006-01-02 15:04:05")).Find(&uwmList)
		}else if u.AllowFriendsToViewTheCircleOfFriends == 3 {
			tx.Where("user_id = ?", u.Id).Where("who_can_see_it_type <> ?", 2).Where("created_at >= ?", time.Now().AddDate(0, -1, 0).Format("2006-01-02 15:04:05")).Find(&uwmList)
		}else if u.AllowFriendsToViewTheCircleOfFriends == 4 {
			tx.Where("user_id = ?", u.Id).Where("who_can_see_it_type <> ?", 2).Where("created_at >= ?", time.Now().AddDate(0, -6, 0).Format("2006-01-02 15:04:05")).Find(&uwmList)
		}
		fmt.Println("uwmList：", uwmList)

		for index, uwm := range uwmList{
			if uwm.WhoCanSeeItType == 3{
				var wci bool = false
				for _, uwci:= range strings.Split(uwm.WhoCanSeeItList, ","){
					if uwci == uId{
						wci = true
					}
				}
				if wci == false{
					uwmList = append(uwmList[:index], uwmList[index+1:]...)
				}
			}
		}

		for _, uwm := range uwmList{
			if uwm.WhoCanSeeItType == 4{
				for index, uwci:= range strings.Split(uwm.WhoCanSeeItList, ","){
					if uwci == uId{
						fmt.Println("uwci：", uwci)
						uwmList = append(uwmList[:index], uwmList[index+1:]...)
					}
					break
				}
			}
		}

		wmList = append(wmList, uwmList...)
	}

	tx.Commit()

	fmt.Println("wmList：", wmList)

	wmList = wechatMoments.BubbleSort(wmList)

	totals := len(wmList)

	if totals < pageSize*pageNo && totals > (pageNo-1)*pageSize{
		return wmList[(pageNo-1)*pageSize:], nil
	}

	if totals <= (pageNo-1)*pageSize{
		return nil, nil
	}

	return wmList[(pageNo-1)*pageSize:pageNo*pageSize], nil
}

